package com.meng.mmlive.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.meng.mmlive.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/14.
 */

public class RefreshView extends View{
    private int circleColor;
    private int circleRadius;
    private int duration;
    private Paint paint;
    public RefreshView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if(attrs != null){
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.RefreshView);
            for(int i = 0;i < typedArray.getIndexCount();i++){
                int index = typedArray.getIndex(i);
                switch (index){
                    case R.styleable.RefreshView_circle_color:
                        circleColor = typedArray.getColor(index, Color.BLUE);
                        break;
                    case R.styleable.RefreshView_circle_radius:
                        circleRadius = typedArray.getDimensionPixelSize(index,
                                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10,getResources().getDisplayMetrics()));
                        break;
                    case R.styleable.RefreshView_duration:
                        duration = typedArray.getInteger(index,500);
                        break;
                }
            }
            typedArray.recycle();
        }
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(circleColor);
        circles.add(new Circle(circleRadius/3,circleRadius,0));
        circles.add(new Circle(circleRadius/3,circleRadius,-circleRadius/3));
        circles.add(new Circle(circleRadius/3,circleRadius,-circleRadius*2/3));
    }
    private List<Circle> circles = new ArrayList<>();
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for(int i = 0;i < circles.size();i++){//1 3 5
            canvas.drawCircle(circleRadius*(2*i+1),circleRadius,circles.get(i).radius,paint);
            circles.get(i).next();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(circleRadius*6,circleRadius*2);
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            invalidate();
            handler.sendEmptyMessageDelayed(0,60);
        }
    };
    private boolean isStart;
    public void startRefresh(){
        if(!isStart){
            handler.sendEmptyMessage(0);
            isStart = true;
        }
    }
    public void stopRefresh(){
        isStart = false;
        handler.removeMessages(0);
        for(Circle circle:circles){
            circle.reset();
        }
    }

    private class Circle{
        public Circle(float minRadius,float maxRadius, float startRadius) {
            this.minRadius = minRadius;
            this.maxRadius = maxRadius;
            this.startRadius = startRadius;
            this.startRadiusTag = startRadius;
        }
        void reset(){
            startRadius = startRadiusTag;
            radius = 0;
        }
        float startRadiusTag;
        float radius;
        float maxRadius;
        float minRadius;
        boolean decrease;
        float startRadius;
        void next(){

            if(startRadius < 0){
                startRadius += 2.5;
                return;
            }
            if(decrease){//递减
                radius -= 2.5;
                if(radius < minRadius){
                    radius = minRadius;
                    decrease = false;
                }
            }else{
                radius += 2.5;
                if(radius > maxRadius){
                    radius = maxRadius;
                    decrease = true;
                }
            }
        }
    }
}
